﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;

namespace QLCCT
{
    public partial class Form1 : Form
    {
        XmlDocument document;       
        Dictionary<String, String> descriptions;

        public Form1()
        {
            InitializeComponent();
            setupListView();
            this.Icon = global::QLCCT.Resource1.Icon1;
            loadDescriptions();
        }

        //  -------------------------------------------------------------------
        void loadDescriptions()
        {
            descriptions = new Dictionary<string, string>();
            document = new XmlDocument();
            document.LoadXml(global::QLCCT.Resource1.Descriptions);

            foreach (XmlNode node in document.ChildNodes[1].ChildNodes)
            {
                descriptions.Add(node.Name.ToLower(), node.InnerText);
            }
        }

        //  -------------------------------------------------------------------
        void setupListView()
        {
            listViewDifferences.View = View.Details;
            listViewDifferences.LabelEdit = true;
            listViewDifferences.AllowColumnReorder = true;
            listViewDifferences.CheckBoxes = false;
            listViewDifferences.FullRowSelect = true;
            listViewDifferences.GridLines = true;
            listViewDifferences.Sorting = SortOrder.Ascending;
            listViewDifferences.Columns.Add("Name", 500, HorizontalAlignment.Left);
            listViewDifferences.Columns.Add("Value 1", 100, HorizontalAlignment.Left);
            listViewDifferences.Columns.Add("Value 2", 100, HorizontalAlignment.Left);
            listViewDifferences.SelectedIndexChanged += new EventHandler(listView1_SelectedIndexChanged);
        }

        //  -------------------------------------------------------------------
        void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewDifferences.SelectedItems.Count == 0)
                return;

            ListViewItem item = listViewDifferences.SelectedItems[0];
            if (item != null)
            {
                if (descriptions.ContainsKey(item.Text))
                {
                    textBoxDesciption.Text = descriptions[item.Text];
                }
                else
                {
                    textBoxDesciption.Text = "";
                }
            }
            else
            {
                textBoxDesciption.Text = "";
            }
        }

        //  -------------------------------------------------------------------
        private void buttonSelect1st_Click(object sender, EventArgs e)
        {
            getFileNameAndWriteToTextBox(textBox1stFilePath);
            executeMatchingIfReady();
        }

        //  -------------------------------------------------------------------
        private void buttonSelect2nd_Click(object sender, EventArgs e)
        {
            getFileNameAndWriteToTextBox(textBox2ndFilePath);
            executeMatchingIfReady();
        }

        //  -------------------------------------------------------------------
        void getFileNameAndWriteToTextBox(TextBox textBox)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "config files (*.cfg)|*.cfg|All files (*.*)|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox.Text = ofd.FileName;
            }
        }

        //  -------------------------------------------------------------------
        void executeMatchingIfReady()
        {
            if (textBox1stFilePath.Text != "" && textBox2ndFilePath.Text != "")
            {
                if (textBox1stFilePath.Text == textBox2ndFilePath.Text)
                {
                    MessageBox.Show("Filenames may not be equal!");
                    return;
                }

                listViewDifferences.Items.Clear();
                listViewDifferences.Items.AddRange(Matching.findMismatches(textBox1stFilePath.Text, textBox2ndFilePath.Text).ToArray());
            }
        }
        
        //  -------------------------------------------------------------------
        private void buttonAbout_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog(this);
        }

        //  -------------------------------------------------------------------
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (listViewDifferences.Items.Count == 0)
                return;

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                using (TextWriter textWriter = new StreamWriter(saveFileDialog.FileName))
                {
                    foreach (ListViewItem item in listViewDifferences.Items)
                    {
                        String name = "                                   ";
                        String first = "                        ";
                        textWriter.WriteLine(
                            item.SubItems[0].Text + name.Remove(0, item.SubItems[0].Text.Length) +
                            "\t" + item.SubItems[1].Text + first.Remove(0, item.SubItems[1].Text.Length) +
                            "\t" + item.SubItems[2].Text);
                    }
                }
            }
        }
    }
}
